<?php
if($this->do=='topic'){
	$this->check_access('global_topic');
	$sql="SELECT * FROM ".DB_PREFIX."topic ORDER BY topic_id DESC";
	$page_size=20;
	$page_current=isset($_GET['page'])&&is_numeric($_GET['page'])?intval($_GET['page']):1;
	$count=$this->db->count($sql);
	$res=$this->db->result($sql." limit ".(($page_current-1)*$page_size).",".$page_size);
	$array=array();
	if($count>0){
		foreach($res as $row){
			$array[$row['topic_id']]['id']=$row['topic_id'];
			$array[$row['topic_id']]['name']=$row['topic_name'];
			$array[$row['topic_id']]['author']=$row['topic_author'];
			$array[$row['topic_id']]['html']=$row['topic_html'];
			$array[$row['topic_id']]['time']=date("Y/m/d H:i:s",$row['topic_time']);
			$array[$row['topic_id']]['comment_count']=$row['topic_comment_count'];
		}
		$parameter='action=global&do=topic&';
		$pagebar=pager(get_self(),$parameter,$page_current,$page_size,$count);
	}else{
		$pagebar="";
	}
	$this->template->in('topic',$array);
	$this->template->in('pagebar',$pagebar);
	$this->template->out('global.topic.list.php');
}
if($this->do=='topic_add'){
	$this->check_access('global_topic');
	$array=array();
	$array['id']=0;
	$array['name']='';
	$array['keywords']='';
	$array['description']='';
	$array['author']='';
	$array['template']='';
	$array['html']='topic-'.(intval($this->db->value(DB_PREFIX."topic","max(topic_id)","1=1 ORDER BY topic_id DESC"))+1);;
	$array['is_comment']=1;
	$array['is_display']=1;
	$this->template->in('topic',$array);
	$this->template->in('mode','insert');
	$this->template->out('global.topic.info.php');
}
if($this->do=='topic_insert'){
	$this->check_access('global_topic');
	$topic_name=empty($_POST['topic_name'])?'':trim(addslashes($_POST['topic_name']));
	$topic_author=empty($_POST['topic_author'])?'':trim(addslashes($_POST['topic_author']));
	$topic_keywords=empty($_POST['topic_keywords'])?'':trim(addslashes($_POST['topic_keywords']));
	$topic_description=empty($_POST['topic_description'])?'':trim(addslashes($_POST['topic_description']));
	$topic_html=empty($_POST['topic_html'])?'':trim(addslashes($_POST['topic_html']));
	$topic_is_comment=empty($_POST['topic_is_comment'])?0:1;
	$topic_is_display=empty($_POST['topic_is_display'])?0:1;
	$array=array();
	$array['topic_name']=$topic_name;
	$array['topic_author']=$topic_author;
	$array['topic_keywords']=$topic_keywords;
	$array['topic_description']=$topic_description;
	$array['topic_html']=$topic_html;
	$array['topic_time']=$_SERVER['REQUEST_TIME'];
	$array['topic_is_comment']=$topic_is_comment;
	$array['topic_is_display']=$topic_is_display;
	$this->db->insert(DB_PREFIX."topic",$array);
	redirect('?action=global&do=topic');
}
if($this->do=='topic_edit'){
	$this->check_access('global_topic');
	$topic_id=empty($_GET['topic_id'])?0:intval($_GET['topic_id']);
	$row=$this->db->row("SELECT * FROM ".DB_PREFIX."topic WHERE topic_id='".$topic_id."'");
	$array=array();
	$array['id']=$row['topic_id'];
	$array['name']=$row['topic_name'];
	$array['keywords']=$row['topic_keywords'];
	$array['description']=$row['topic_description'];
	$array['author']=$row['topic_author'];
	$array['template']=$row['topic_template'];
	$array['html']=$row['topic_html'];
	$array['is_comment']=$row['topic_is_comment'];
	$array['is_display']=$row['topic_is_display'];
	$this->template->in('topic',$array);
	$this->template->in('mode','update');
	$this->template->out('global.topic.info.php');
}
if($this->do=='topic_update'){
	$this->check_access('global_topic');
	$topic_id=empty($_POST['topic_id'])?0:intval($_POST['topic_id']);
	$topic_name=empty($_POST['topic_name'])?'':trim(addslashes($_POST['topic_name']));
	$topic_author=empty($_POST['topic_author'])?'':trim(addslashes($_POST['topic_author']));
	$topic_keywords=empty($_POST['topic_keywords'])?'':trim(addslashes($_POST['topic_keywords']));
	$topic_description=empty($_POST['topic_description'])?'':trim(addslashes($_POST['topic_description']));
	$topic_html=empty($_POST['topic_html'])?'':trim(addslashes($_POST['topic_html']));
	$topic_is_comment=empty($_POST['topic_is_comment'])?0:1;
	$topic_is_display=empty($_POST['topic_is_display'])?0:1;
	$array=array();
	$array['topic_name']=$topic_name;
	$array['topic_author']=$topic_author;
	$array['topic_keywords']=$topic_keywords;
	$array['topic_description']=$topic_description;
	$array['topic_html']=$topic_html;
	$array['topic_is_comment']=$topic_is_comment;
	$array['topic_is_display']=$topic_is_display;
	$this->db->update(DB_PREFIX."topic",$array,"topic_id=$topic_id");
	redirect('?action=global&do=topic');
}
if($this->do=='topic_delete'){
	$this->check_access('global_topic');
	$topic_id=empty($_POST['topic_id'])?0:$_POST['topic_id'];
	$this->db->delete(DB_PREFIX."topic","topic_id=".$topic_id."");
	redirect('?action=global&do=topic');
}